package org.iiitb.assignment.db;

import java.sql.*;



public class MySQLConnector {
	//Connection con = null;
    //Statement stmt = null;
    static String database = "travel_world";
    static String user = "root";
    static String pword = "root";
	Connection con = null;
    static MySQLConnector mysql = new MySQLConnector();
	
	private MySQLConnector(){
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String ConnectionString="jdbc:mysql://127.0.0.1:3306/" + database;
            System.out.println(ConnectionString);
            con = DriverManager.getConnection(ConnectionString,user,pword);
        }catch(SQLException SQLEx) {
            System.out.println("MySQL error: "+SQLEx.getErrorCode()+
                  " SQLSTATE:" +SQLEx.getSQLState());
            System.out.println(SQLEx.getMessage());
        }catch(Exception ex){
            System.out.println(ex.getMessage());
        }
	}
	
	public static MySQLConnector getNewInstance(){
		return mysql;
	}
	
	public static Connection getConnection(){
		return mysql.con;
	}
	
	public ResultSet executeQuery(String query){
		
		ResultSet result = null;
		Connection con = getConnection();
		if(con == null) return result;
		try {
			Statement stmt = con.createStatement();
			System.out.println(query);
			result = stmt.executeQuery(query);
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}
		
		return result;	


	}

	public int updateQuery(String query){

		int result = 0;
		Connection con = getConnection();
		if(con == null) return result;
		try {
			Statement stmt = con.createStatement();
			System.out.println(query);
			result = stmt.executeUpdate(query);
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}


		return result;	
	}
}